// $(function(){
//     $(".widget-popular-posts").clonefn({
//         cloneto:false,//是否允许克隆
//         initial:"top", //初始位置top或down
//         speed:800,//动画速度
//         easing:"easeOutQuad",
//     });
// });

// 访问新闻 API
window.onload = function () {
    getLargePost();
    getLatestPost();
    getPopularPost();
}

// 轮播图
function getLargePost() {
    $.ajax({
        type: "get",
        async: false,
        url: "http://api.tianapi.com/it/index",
        data: {
            key: "7eaf449f47ca4b5251ae3123adc8220d",
            num: 6,
            // rand: 0,
        },
        success: function (data) {
            console.log("轮播图");

            let $largePostList = $("#largePostList .swiper");
            let $point = $("#largePostList .swiper-point-item");
            $largePostList.empty();
            $point.empty();

            $.each(data.newslist, function (i, item) {

                $largePostList.append(
                    "<div class='swiper-item'>" +
                    "<img onclick='toNewPage(\"" + item.url + "\")' src='" + item.picUrl + "'>" +
                    "<p onclick='toNewPage(\"" + item.url + "\")'>" + item.title + "</p>" +
                    "</div>"
                )

                let point = ""
                if (i === 0) {
                    point = "<div class='point active'></div>";
                } else {
                    point = "<div class='point'></div>"
                }
                $point.append(
                    point
                )
            })
            let sw = new Swiper();
            sw.init();
        },
        error: function (err) {
            console.log(err);
        }
    });
}

// 最新新闻
function getLatestPost() {
    $.ajax({
        type: "get",
        async: false,
        url: "http://api.tianapi.com/it/index",
        data: {
            key: "7eaf449f47ca4b5251ae3123adc8220d",
            num: 8,
            // rand: 0,
            page: 2,
        },
        success: function (data) {
            console.log("最新新闻");

            let $latestPostList = $("#latestPostList");
            $latestPostList.empty();

            $.each(data.newslist, function (i, item) {

                $latestPostList.append(
                    "<div class='col-lg-6'>" +
                    "<article class='entry'>" +
                    "<div class='entry__img-holder'>" +
                    "<a href='" + item.url + "'>" +
                    "<div class='thumb-container'>" +
                    "<img src='" + item.picUrl + "'" +
                    " class='entry__img lazyload' alt=''/>" +
                    "</div>" +
                    "</a>" +
                    "</div>" +

                    "<div class='entry__body'>" +
                    "<div class='entry__header'>" +
                    "<h2 class='entry__title'>" +
                    "<a href='" + item.url + "'>" + item.title + "</a>" +
                    "</h2>" +
                    "<ul class='entry__meta'>" +
                    "<li class='entry__meta-date'>" +
                    item.ctime +
                    "</li>" +
                    "<li class=entry__meta-author>" +
                    "by <a>" + item.source + "</a>" +
                    "</li>" +
                    "</ul>" +
                    "</div>" +
                    "<div class='entry__excerpt'>" +
                    "<p>" + item.description + "</p>" +
                    "</div>" +
                    "</div>" +
                    "</article>" +
                    "</div>"
                )
            })
        },
        error: function (err) {
            console.log(err);
        }
    });
}

// 侧边栏
function getPopularPost() {
    $.ajax({
        type: "get",
        async: false,
        url: "http://api.tianapi.com/internet/index",
        data: {
            key: "7eaf449f47ca4b5251ae3123adc8220d",
            num: 5,
            // rand: 0,
        },
        success: function (data) {
            console.log("侧边栏");

            let $popularPostList = $("#popularPostList");
            // $popularPostList.empty();

            $.each(data.newslist, function (i, item) {

                $popularPostList.append(
                    "<li>" +
                    "<article class='clearfix'>" +
                    "<div class='widget-popular-posts__img-holder'>" +
                    "<span class='widget-popular-posts__number'>" + (i + 1) + "</span>" +
                    "<div class='thumb-container'>" +
                    "<a href='" + item.url + "'>" +
                    "<img src='" + item.picUrl + "' alt=''" +
                    "class='lazyload'>" +
                    "</a>" +
                    "</div>" +
                    "</div>" +
                    "<div class='widget-popular-posts__entry'>" +
                    "<h3 class='widget-popular-posts__entry-title'>" +
                    "<a style='' href='" + item.picUrl + "'>" + item.title + "</a>" +
                    "</h3>" +
                    "<p style='text-align: right'>"+item.ctime+"</p>"+
                    "</div>" +
                    "</article>" +
                    "</li>"
                )
            })
        },
        error: function (err) {
            console.log(err);
        }
    });
}


function toNewPage(url) {
    window.location.href = url;
}